<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页 - Asurada SSO客户端</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        .hero-section {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 80px 0;
            margin-bottom: 50px;
        }
        .feature-card {
            transition: transform 0.3s ease;
            height: 100%;
        }
        .feature-card:hover {
            transform: translateY(-5px);
        }
        .user-info-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
        }
        .stats-card {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="bi bi-shield-check"></i>
                Asurada SSO客户端
            </a>
            
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link active" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/protected">受保护页面</a>
                    </li>
                    <li class="nav-item" th:if="${isLoggedIn}">
                        <a class="nav-link" href="/profile">个人信息</a>
                    </li>
                </ul>
                
                <ul class="navbar-nav">
                    <li class="nav-item" th:if="${!isLoggedIn}">
                        <a class="nav-link" href="/login">
                            <i class="bi bi-box-arrow-in-right"></i>
                            登录
                        </a>
                    </li>
                    <li class="nav-item dropdown" th:if="${isLoggedIn}">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="bi bi-person-circle"></i>
                            <span th:text="${user?.username} ?: '用户'">用户</span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="/profile">
                                <i class="bi bi-person"></i> 个人信息
                            </a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="#" onclick="logout()">
                                <i class="bi bi-box-arrow-right"></i> 登出
                            </a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <main>
        <!-- Hero区域 -->
        <section class="hero-section">
            <div class="container">
                <div class="row align-items-center">
                    <div class="col-lg-6">
                        <h1 class="display-4 fw-bold mb-4">
                            欢迎使用 Asurada SSO
                        </h1>
                        <p class="lead mb-4">
                            这是一个基于Spring Boot 3.5.3和JDK 21构建的单点登录客户端演示应用。
                            体验安全、便捷的统一身份认证服务。
                        </p>
                        <div th:if="${!isLoggedIn}">
                            <a href="/login" class="btn btn-light btn-lg me-3">
                                <i class="bi bi-box-arrow-in-right"></i>
                                立即登录
                            </a>
                            <a href="/protected" class="btn btn-outline-light btn-lg">
                                <i class="bi bi-shield-lock"></i>
                                体验保护页面
                            </a>
                        </div>
                        <div th:if="${isLoggedIn}">
                            <a href="/protected" class="btn btn-light btn-lg me-3">
                                <i class="bi bi-shield-check"></i>
                                访问保护页面
                            </a>
                            <a href="/profile" class="btn btn-outline-light btn-lg">
                                <i class="bi bi-person"></i>
                                查看个人信息
                            </a>
                        </div>
                    </div>
                    <div class="col-lg-6 text-center">
                        <i class="bi bi-shield-check" style="font-size: 200px; opacity: 0.8;"></i>
                    </div>
                </div>
            </div>
        </section>

        <div class="container">
            <!-- 用户状态卡片 -->
            <div class="row mb-5" th:if="${isLoggedIn}">
                <div class="col-md-8">
                    <div class="card user-info-card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <i class="bi bi-person-check"></i>
                                欢迎回来，<span th:text="${user.username}">用户</span>！
                            </h5>
                            <p class="card-text">您已成功通过SSO认证登录系统。</p>
                            <div class="row">
                                <div class="col-sm-6">
                                    <small>用户ID: <span th:text="${user.id}">1</span></small>
                                </div>
                                <div class="col-sm-6">
                                    <small>邮箱: <span th:text="${user.email}">user@example.com</span></small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card stats-card">
                        <div class="card-body text-center">
                            <h5 class="card-title">
                                <i class="bi bi-clock"></i>
                                会话状态
                            </h5>
                            <p class="card-text">
                                <span class="badge bg-success">在线</span>
                            </p>
                            <small>令牌有效期：30分钟</small>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 功能特性 -->
            <div class="row mb-5">
                <div class="col-12">
                    <h2 class="text-center mb-5">系统特性</h2>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card feature-card">
                        <div class="card-body text-center">
                            <i class="bi bi-shield-check text-primary" style="font-size: 3rem;"></i>
                            <h5 class="card-title mt-3">安全认证</h5>
                            <p class="card-text">
                                采用JWT令牌机制，支持访问令牌和刷新令牌，确保认证安全性。
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card feature-card">
                        <div class="card-body text-center">
                            <i class="bi bi-arrow-repeat text-success" style="font-size: 3rem;"></i>
                            <h5 class="card-title mt-3">单点登录</h5>
                            <p class="card-text">
                                一次登录，多处使用。与SSO服务端无缝集成，提供统一身份认证。
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card feature-card">
                        <div class="card-body text-center">
                            <i class="bi bi-speedometer2 text-warning" style="font-size: 3rem;"></i>
                            <h5 class="card-title mt-3">高性能</h5>
                            <p class="card-text">
                                基于Redis缓存，支持令牌本地验证，减少网络请求，提升响应速度。
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 快速操作 -->
            <div class="row mb-5">
                <div class="col-12">
                    <h2 class="text-center mb-4">快速操作</h2>
                </div>
                <div class="col-md-6 mb-3">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <i class="bi bi-shield-lock text-primary"></i>
                                访问受保护页面
                            </h5>
                            <p class="card-text">
                                体验需要登录才能访问的页面，测试SSO认证功能。
                            </p>
                            <a href="/protected" class="btn btn-primary">
                                <i class="bi bi-arrow-right"></i>
                                立即访问
                            </a>
                        </div>
                    </div>
                </div>
                <div class="col-md-6 mb-3">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <i class="bi bi-person text-info"></i>
                                查看个人信息
                            </h5>
                            <p class="card-text" th:if="${isLoggedIn}">
                                查看和管理您的个人账户信息。
                            </p>
                            <p class="card-text" th:if="${!isLoggedIn}">
                                登录后可以查看和管理您的个人账户信息。
                            </p>
                            <a th:href="${isLoggedIn} ? '/profile' : '/login?redirect=/profile'" class="btn btn-info">
                                <i class="bi bi-arrow-right"></i>
                                <span th:text="${isLoggedIn} ? '查看信息' : '登录查看'">查看信息</span>
                            </a>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 技术信息 -->
            <div class="row mb-5">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="mb-0">
                                <i class="bi bi-info-circle"></i>
                                技术架构信息
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <h6>后端技术栈</h6>
                                    <ul class="list-unstyled">
                                        <li><i class="bi bi-check-circle text-success"></i> Spring Boot 3.5.3</li>
                                        <li><i class="bi bi-check-circle text-success"></i> JDK 21</li>
                                        <li><i class="bi bi-check-circle text-success"></i> Spring Security</li>
                                        <li><i class="bi bi-check-circle text-success"></i> JWT (JSON Web Token)</li>
                                        <li><i class="bi bi-check-circle text-success"></i> Redis缓存</li>
                                    </ul>
                                </div>
                                <div class="col-md-6">
                                    <h6>前端技术栈</h6>
                                    <ul class="list-unstyled">
                                        <li><i class="bi bi-check-circle text-success"></i> Thymeleaf模板引擎</li>
                                        <li><i class="bi bi-check-circle text-success"></i> Bootstrap 5</li>
                                        <li><i class="bi bi-check-circle text-success"></i> Bootstrap Icons</li>
                                        <li><i class="bi bi-check-circle text-success"></i> Axios HTTP客户端</li>
                                        <li><i class="bi bi-check-circle text-success"></i> 响应式设计</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- 页脚 -->
    <footer class="bg-light py-4 mt-5">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <p class="text-muted mb-0">
                        &copy; 2024 Asurada SSO客户端演示应用
                    </p>
                </div>
                <div class="col-md-6 text-end">
                    <p class="text-muted mb-0">
                        基于Spring Boot 3.5.3 + JDK 21构建
                    </p>
                </div>
            </div>
        </div>
    </footer>

    <!-- JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    
    <script>
        // 登出函数
        function logout() {
            if (confirm('确定要登出吗？')) {
                axios.post('/sso/logout')
                    .then(function (response) {
                        if (response.data.code === 200) {
                            window.location.href = '/';
                        } else {
                            alert('登出失败：' + response.data.message);
                        }
                    })
                    .catch(function (error) {
                        console.error('登出请求失败:', error);
                        alert('登出失败，请重试');
                    });
            }
        }
        
        // 页面加载完成后检查登录状态
        document.addEventListener('DOMContentLoaded', function() {
            // 如果已登录，可以定期检查令牌状态
            if (document.querySelector('.user-info-card')) {
                // 每5分钟检查一次登录状态
                setInterval(function() {
                    axios.get('/sso/check')
                        .then(function (response) {
                            if (response.data.code === 200 && !response.data.data) {
                                // 如果令牌已失效，提示用户重新登录
                                if (confirm('您的登录已过期，是否重新登录？')) {
                                    window.location.href = '/login';
                                }
                            }
                        })
                        .catch(function (error) {
                            console.error('检查登录状态失败:', error);
                        });
                }, 5 * 60 * 1000); // 5分钟
            }
        });
    </script>
</body>
</html>